/*
    http://bhepple.freeshell.org

    Copyright (C) 2010 Bob Hepple <bhepple@freeshell.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

*/

/**
 * \file tbm.c
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <tbs.h>
#include <tba.h>
#include <tbm.h>

tbs tbm_backtick(const tba command_in) {
    int fd[2];
    int pid;
    tbs retval = NULL;

    if (pipe(fd) < 0)
        fprintf(stderr, "Can't open a pipe.\n");
    else {
        if ((pid = vfork()) == -1) {
            fprintf(stderr, "Fork failed.\n");
        } else if (pid == 0) { 
            char **command = (char **)command_in;
            // we're the child - put stdout on pipe
            close(fd[0]); /* child does not need to read from pipe */
            close(1); /* set child's stdout to be the pipe */
            dup(fd[1]);
            close(fd[1]);
            // setvbuf(1, NULL, _IONBF, 0); // unbuffered output
            execvp(*command, command);
            _exit(1); /* abandon hope all ye who get here */
        } else { 
            // we're still the PARENT - read from pipe
            FILE *p;
            close(fd[1]); /* parent does not write to pipe */
            if ((p = fdopen(fd[0], "r")) == NULL) {
                fprintf(stderr, "Can't read from pipe\n");
            } else {
#define BUFSIZE 80
                char inbuf[BUFSIZE];
                int status;
                
                while (fgets(inbuf, BUFSIZE, p) != NULL) {
                    tbs_append_str(&retval, inbuf);
                }
                wait(&status);

                fclose(p); /* no need to close fd[1] too */
            }
        }
    }
    return(retval);
}

tbs tbm_system(const tbs command, int * status_out) {
    int fd[2];
    int pid;
    tbs retval = NULL;

    if (pipe(fd) < 0)
        fprintf(stderr, "Can't open a pipe.\n");
    else {
        if ((pid = vfork()) == -1) {
            fprintf(stderr, "Fork failed.\n");
        } else if (pid == 0) { 
            // we're the child - put stdout on pipe
            close(fd[0]); /* child does not need to read from pipe */
            close(1); /* set child's stdout to be the pipe */
            dup(fd[1]);
            close(fd[1]);
            // setvbuf(1, NULL, _IONBF, 0); // unbuffered output
            _exit(system(tbs_get_str(command)));
        } else { 
            // we're still the PARENT - read from pipe
            FILE *p;
            close(fd[1]); /* parent does not write to pipe */
            if ((p = fdopen(fd[0], "r")) == NULL) {
                fprintf(stderr, "Can't read from pipe\n");
            } else {
#define BUFSIZE 80
                char inbuf[BUFSIZE];
                int status;

                while (fgets(inbuf, BUFSIZE, p) != NULL) {
                    tbs_append_str(&retval, inbuf);
                }
                wait(&status);
                // printf("\nstatus=%d\n", status);
                if (status_out) *status_out = status;

                fclose(p); /* no need to close fd[1] too */
            }
        }
    }
    return(retval);
}

